<template>
<el-container class="home-contianer">
    <!--头部区域-->
  <el-header>
      <div>
          <img src="../assets/图片.jpg" alt="" width="50px" height="50px" style="border-radius: 50%;">
          <span>电商后台管理系统</span>
      </div>
      

      <el-button type="info" @click="logout">退出</el-button>
  </el-header>
  
  <el-container>
    <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="btn" @click="togCollapse">|||</div>
        <el-menu
            router
            :collapse="isCollapse"
            :collapse-transition="false"
            unique-opened
            :default-active="activePath"
            class="el-menu-vertical-demo"
            background-color="#545c64"
            text-color="#fff"
            active-text-color="#ffd04b">
            <el-submenu :index="item.id + ''" v-for="item in menulist" :key="item.id">
                <template slot="title">
                <i :class="iconObj[item.id]"></i>
                <span>{{item.authName}}</span>
                </template>
                <el-menu-item @click="saveNavState('/' + subitem.path)" :index="'/' + subitem.path" v-for="subitem in item.children" :key="subitem.id">
                    <template slot="title">
                        <i class="el-icon-menu"></i>
                        <span>{{subitem.authName}}</span>
                    </template>
                </el-menu-item>           
            </el-submenu>
        </el-menu>
    </el-aside>
    
    <el-main>
        <router-view></router-view>
    </el-main>
  </el-container>
</el-container>
  
</template>

<script>

export default {
    data () {
        return {
            menulist:[],
            iconObj:{
                '125':'el-icon-user-solid',
                '103':'el-icon-menu',
                '101':'el-icon-shopping-bag-1',
                '102':'el-icon-notebook-1',
                '145':'el-icon-document'
            },
            isCollapse: false,
            //被激活的链接地址
            activePath:'',
        }
    },
    created(){
        this.getMenuList()
        this.activePath = window.sessionStorage.getItem('activePath')
    },
    methods:{
        logout(){
            window.sessionStorage.clear()
            this.$router.push('/login')
        },
        async getMenuList(){
            const {data:res} = await this.$http.get('menus')
            if(res.meta.status !== 200) return this.$message.error(res.meta.msg)
            this.menulist = res.data
            console.log(res)
        },
        togCollapse(){
            this.isCollapse = !this.isCollapse
        },
        saveNavState(activePath){
            window.sessionStorage.setItem('activePath',activePath)
            this.activePath = activePath
        }
    }
}
</script>

<style lang="less" scoped>
.home-contianer{
    height: 100%;
}
.el-header{
    background-color: #333;
    display: flex;
    justify-content: space-between;
    align-items: center;
    color: lightgray;
    font-size: 20px;
    >div{
        display: flex;
        align-items: center;
        span{
            margin-left: 15px;
        }
    }
}
.el-aside{
    background-color: #333;
    .el-menu{
        border: null;
    }
}
.el-main{
    background-color: #eaedf1;
}
.iconfont{
    margin-right: 10px;
}
.btn{
    background-color: #4a5064;
    font-size: 10px;
    line-height: 24px;
    color: #fff;
    text-align: center;
    letter-spacing: 0.2em;
    cursor: pointer;
}
</style>